package com.atguigu.structures.tree.huffman;

import com.atguigu.structures.tree.node.HuffmanNode;

import java.util.Arrays;

/**
 * 哈夫曼案例测试类
 *
 * @author xr-8
 * @date 2022-12-04
 * @time 23:17
 */
public class HuffmanTreeTest {

    public static void main(String[] args) {
//        testHuffmanTree();
        testHuffmanCode();
//        testHuffmanFileCode();
    }

    /**
     * 测试哈夫曼树
     */
    public static void testHuffmanTree() {
        int arr[] = { 13, 7, 8, 3, 29, 6, 1 };
        HuffmanNode root = HuffmanTree.createHuffmanTree(arr);
        HuffmanTree.preOrder(root);
    }

    /**
     * 测试哈夫曼编码
     */
    public static void testHuffmanCode() {
        // 压缩
        String content = "i like like like java do you like a java";//"hello world"
//        String content = "hello world";
		byte[] contentBytes = content.getBytes();
		System.out.println("初始字符串长度是:" + contentBytes.length); //40

		byte[] huffmanCodesBytes= HuffmanCode.huffmanZip(contentBytes);
		System.out.println("压缩后的结果是:" + Arrays.toString(huffmanCodesBytes) + " 长度= " + huffmanCodesBytes.length);

		// 解码
		byte[] sourceBytes = HuffmanCode.decode(HuffmanCode.huffmanCodes, huffmanCodesBytes);
		System.out.println("原来的字符串=" + new String(sourceBytes)); // "i like like like java do you like a java"
    }

    /**
     * 测试赫夫曼压缩解压文件
     */
    public static void testHuffmanFileCode(){
        // 测试压缩文件
		String srcFile = "d://Uninstall.xml";
		String dstFile = "d://Uninstall.zip";
        HuffmanCode.zipFile(srcFile, dstFile);
		System.out.println("压缩文件ok~~");

        // 测试解压文件
        String zipFile = "d://Uninstall.zip";
        String dstFile1 = "d://Uninstall2.xml";
        HuffmanCode.unZipFile(zipFile, dstFile1);
        System.out.println("解压成功!");
    }
}
